package com.ja.openchat.ui.chat.group;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.zy.openchat.core.manager.TgEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.goldze.mvvmhabit.base.BaseActivity;

public class SettingFragment extends Fragment{
        public Context mContext;
        protected Unbinder unbinder;
        protected View rootView;

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            mContext = getActivity();

            //注册事件
            if (isHaveEventBus()) {
                if (!EventBus.getDefault().isRegistered(this)) {
                    EventBus.getDefault().register(this);
                }
            }

            initDataAndEvent(savedInstanceState);
        }

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            int layout = initLayout();
            if (layout != 0) {
                rootView = onLayoutInflaterView(inflater,container);
                unbinder = ButterKnife.bind(this, rootView);
                initView(rootView);
                return rootView;
            }else {
                return super.onCreateView(inflater, container, savedInstanceState);
            }
        }

        protected void initView(View view) {

        }

        @Override
        public void onAttach(@NonNull Context context) {
            super.onAttach(context);
            mContext = context;
        }

        protected View onLayoutInflaterView(@NonNull LayoutInflater inflater, ViewGroup parentView){
            return inflater.inflate(initLayout(), parentView, false);
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            if (EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().unregister(this);
            }
        }

        public void showProgress() {
            Activity avy = getActivity();
            if (avy != null) {
                if (avy instanceof BaseActivity) {
                    BaseActivity bavy = (BaseActivity) avy;
                    bavy.showProgress();
                }
            }
        }

        public synchronized void hideProgress() {
            Activity avy = getActivity();
            if (avy != null) {
                if (avy instanceof BaseActivity) {
                    BaseActivity bavy = (BaseActivity) avy;
                    bavy.hideProgress();
                }
            }
        }

        public void showWarnTip(String tip) {
            Activity avy = getActivity();
            if (avy != null) {
                Toast.makeText(getActivity(), tip, Toast.LENGTH_SHORT).show();
            }
        }

        public void showErrorTip(String error) {
            Activity avy = getActivity();
            if (avy != null) {
                Toast.makeText(getActivity(), error, Toast.LENGTH_SHORT).show();
            }
        }

        @Subscribe(threadMode = ThreadMode.MAIN)
        public void onTgEvent(final TgEvent tg_event) {
        }

        /**
         * 初始化布局
         *
         * @return 布局
         */
        protected int initLayout(){
            return 0;
        }

        /**
         * EventBus开关
         */
        protected boolean isHaveEventBus() {
            return false;
        }

        /**
         * 初始化数据
         *
         * @param savedInstanceState 数据状态
         */
        protected void initDataAndEvent(Bundle savedInstanceState){}

}
